﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace tab2sql
{

    /// <summary>
    /// General process :
    /// 1. User pastes in csv/tsv as usual
    /// 2. Presses tab2sql
    /// 3. Presses ImportSurvey
    /// 4. We create table - non temporary object with autogen name - e.g. Stage.Flats9834503
    /// 5. Compare DDL with existing objects.
    /// 6. If no problems, copy across info, filtering for dupes as appropriate.
    /// </summary>









    public partial class importSurvey : Form
    {
        /// <summary>
        /// Options object.
        /// </summary>
        public tab2sqlOptions myOptions = new tab2sqlOptions();
        /// <summary>
        /// main object. 
        /// </summary>
        public tab2sql myObject;

        BindingSource bs = new BindingSource();
        DataTable dt = new DataTable();
        /// <summary>
        /// Constructor. 
        /// </summary>
        public importSurvey()
        {

            InitializeComponent();
            myObject = new tab2sql();
            TSVtoDataTable tsv2DT = new TSVtoDataTable();
            tsv2DT.myObject = myObject;
            myOptions.Delimiter = "\t";
            tsv2DT.myOptions = myOptions;
            tsv2DT.Convert2DT(Global.inputBunk);
            dt = tsv2DT.dt;

            navControls.BindingSource = bs;
            bs.DataSource = dt;
            myGridView.DataSource = bs;
            myGridView.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        public DataTable ExtractDataTable(DataGridView dgv)
        {
            BindingSource BSrc = dgv.DataSource as BindingSource;
            DataTable dtSrc = BSrc.DataSource as DataTable;
            DataTable dtDest = null;
            if (dtSrc != null)
            {
                dtDest = new DataTable();
                dtDest.Merge(dtSrc);
            }
            return dtDest;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            myGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
            myOptions.Delimiter = "\t";
            DataTable dt = new DataTable();
            dt = ExtractDataTable(myGridView);
            DataTable2SQL DTS = new DataTable2SQL();
            DTS.myOptions = myOptions;
            DTS.myObject = myObject;
            DTS.GenerateSQLStatement(dt);
            System.Text.StringBuilder endS = DTS.endS;
            dt = null;
            TextBox TB = ((MainForm)this.Owner).outStr;
            TB.Text = endS.ToString();
            this.Close();
            this.Dispose();

        }
        private void bindingNavigator1_RefreshItems(object sender, EventArgs e)
        {
            // wtf
        }

        private void showGrid_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'surveys2DataSet.systemObjects' table. You can move, or remove it, as needed.
            this.systemObjectsTableAdapter1.Fill(this.surveys2DataSet.systemObjects);
  

        }

        private void button4_Click(object sender, EventArgs e)
        {

            

            DataRowView dr = (DataRowView)tblListing.SelectedItem;
            DataRow d2 = dr.Row;
            string schema = d2.Field<string>(0);
            string table = d2.Field<string>(1);
            MessageBox.Show(table);
            DataTable dtQ = new DataTable();
            db DB = new db();
             dtQ = DB.RenderSQL("SELECT * FROM Surveys2."+schema+"."+table);
             dataGridView2.DataSource = dtQ;
             dataGridView2.Refresh();
  
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataRowView dr = (DataRowView)tblListing.SelectedItem;
            DataRow d2 = dr.Row;
            string schema = d2.Field<string>(0);
            string table = d2.Field<string>(1);
            DataTable dtQ = new DataTable();
            db DB = new db();
            dtQ = DB.RenderSQL("SELECT * FROM Surveys2." + schema + "." + table);
            dataGridView2.DataSource = dtQ;
            dataGridView2.Refresh();

            dt.Merge(dtQ);
            navControls.BindingSource = bs;
            bs.DataSource = dt;
            myGridView.DataSource = bs;
            myGridView.Refresh();
        }
    }
}
